package com.nineclock.approve.handler;

import com.alibaba.fastjson.JSONObject;
import com.nineclock.approve.utils.ActivitiUtils;
import com.nineclock.common.utils.UUIDUtils;
import org.activiti.bpmn.model.Process;
import org.activiti.bpmn.model.SequenceFlow;

/**
 * 节点处理器抽象类
 */
public abstract class ProcessNodeResolver {

    /**
     * 是否由该处理器执行
     * @return
     */
    public abstract boolean support(String nodeType);


    /**
     * 处理节点
     * @param process
     * @param curNode 当前节点
     * @param nextNode 下个节点
     */
    public abstract void addFlowNodeToActiviti(Process process, JSONObject curNode, JSONObject nextNode);

    /**
     * 获取当前节点的Key/ID
     * @param currNode
     * @return
     */
    protected String getNodeId(JSONObject currNode) {
        String type = currNode.getString("type");
        String nodekey = currNode.getString("nodeKey");
        return ActivitiUtils.getKey(type, nodekey);
    }

    /**
     * 创建流程图中的连线
     * @param process 流程对象
     * @param curNode 当前节点
     * @param nextNode 下一个节点
     * @return
     */
    protected void newSequenceFlow(Process process, JSONObject curNode, JSONObject nextNode) {
        // 绘制连线
        SequenceFlow sequenceFlow = new SequenceFlow();
        sequenceFlow.setId(ActivitiUtils.SEQUENCEFLOW_PREFIX + UUIDUtils.getUUID());
        sequenceFlow.setSourceRef(getNodeId(curNode));
        sequenceFlow.setTargetRef(getNodeId(nextNode));

        process.addFlowElement(sequenceFlow);
    }

}
